package com.milotnt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.milotnt.mapper.CoachMapper;
import com.milotnt.pojo.ClassOrder;
import com.milotnt.pojo.ClassTable;
import com.milotnt.pojo.ClassTableDto;
import com.milotnt.pojo.Coach;
import com.milotnt.service.ClassTableService;
import com.milotnt.service.CoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CoachServiceImpl extends ServiceImpl<CoachMapper, Coach> implements CoachService {

    @Autowired
    private ClassTableService tableService;
    @Override
    public List<Coach> findAll() {
        return this.list();
    }

    @Override
    public Integer selectTotalCount() {
        return Math.toIntExact(this.count());
    }

    @Override
    public void insertCoach(Coach coach) {
        this.save(coach);
    }

    @Override
    public Coach selectByCoachId(Integer coachId) {
        return this.getById(coachId);
    }

    @Override
    public void updateCoach(Coach coach) {
        this.updateById(coach);
    }

    @Override
    public String deleteByCoachId(Integer coachId) {
        LambdaQueryWrapper<ClassTable> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ClassTable::getCoachId, coachId);
        long count = tableService.count(lqw);
        // 当前教练还有需要教授的课程，无法进行解雇
        if(count > 0){
            return "有需教授的课程，暂时无法解雇";
        }else {
            this.removeById(coachId);
            return null;
        }
    }

    @Override
    public Coach coachLogin(Coach coach) {
        LambdaQueryWrapper<Coach> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Coach::getCoachId, coach.getCoachId());
        lqw.eq(Coach::getCoachPassword, coach.getCoachPassword());
        return this.getOne(lqw);
    }
}
